//
//  MJTabBarController.m
//  MinJin
//
//  Created by 迟建国 on 2023/3/22.
//

#import "MJTabBarController.h"
#import "BaseViewController.h"
#import "MJTabBar.h"

#import "HomeViewController.h"
#import "MineViewController.h"
#import "WorkViewController.h"
#import "BaseNavigationViewController.h"

@interface MJTabBarController ()
@property(strong,nonatomic)UIWindow *window;

@property(strong,nonatomic)UIButton *button;
@end

@implementation MJTabBarController

//手势事件 －－ 改变位置
-(void)changePostion:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan translationInView:_button];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height - SafeAreaBottomHeight - 60;
    CGRect originalFrame = _button.frame;
    if (originalFrame.origin.x >= 0 && originalFrame.origin.x+originalFrame.size.width <= width) {
        originalFrame.origin.x += point.x;
    }if (originalFrame.origin.y >= 0 && originalFrame.origin.y+originalFrame.size.height <= height) {
        originalFrame.origin.y += point.y;
    }
    _button.frame = originalFrame;
    [pan setTranslation:CGPointZero inView:_button];
    if (pan.state == UIGestureRecognizerStateBegan) {
        _button.enabled = NO;
    }else if (pan.state == UIGestureRecognizerStateChanged){
    } else {
        CGRect frame = _button.frame;
        //是否越界
        BOOL isOver = NO;
        if (frame.origin.x < 0) {
            frame.origin.x = 0;
            isOver = YES;
        } else if (frame.origin.x+frame.size.width > width) {
            frame.origin.x = width - frame.size.width;
            isOver = YES;
        }if (frame.origin.y < 0) {
            frame.origin.y = 0;
            isOver = YES;
        } else if (frame.origin.y+frame.size.height > height) {
            frame.origin.y = height - frame.size.height;
            isOver = YES;
        }if (isOver) {
            [UIView animateWithDuration:0.3 animations:^{
                self.button.frame = frame;
            }];
        }
        _button.enabled = YES;
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.button removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    WorkViewController *workVC = [[WorkViewController alloc] init];
    //MineViewController *mineVC = [[MineViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[MineViewController alloc] init]];
    [self setupChildVC:homeVC title:@"首页" image:@"homeDefult" selectImage:@"homeSelect"];
    [self setupChildVC:workVC title:@"作业" image:@"workDefult" selectImage:@"workSelect"];
    [self setupChildVC:nav title:@"我" image:@"mineDefult" selectImage:@"mineSelect"];
    MJTabBar * tabbar = [[MJTabBar alloc] init];
    [self setValue:tabbar forKeyPath:@"tabBar"];
    
    //    [self performSelector:@selector(createButton) withObject:nil afterDelay:0];
    
    // Do any additional setup after loading the view.
}

- (void)setupChildVC:(BaseViewController *)vc title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage{
    
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    vc.statusBarDefault = NO;
//    BaseNavigationViewController *navigationVC = [[BaseNavigationViewController alloc] initWithRootViewController:vc];
    [self addChildViewController:vc];
    
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
}
- (BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
@end
